﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace FinanceCalculators
{
    public class MortageSaveData
    {
        List<MortageProperties> myprop = new List<MortageProperties>();
        IsolatedStorageFile isostore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isostream;
        XDocument xdoc;
        public void AddDetails(int TermOfLoan, double IntrestRate, double LoanAmount, double Payment, double Price, double DownPayment)
        {
            try
            {
                if (!isostore.FileExists("TaxDetails.xml"))
                {
                    isostream = new IsolatedStorageFileStream("TaxDetails.xml", FileMode.Create, isostore);


                    xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                         new XElement("TaxDetails", new XAttribute("id", 1),
                                    new XElement("TermOfLoan", TermOfLoan),
                                         new XElement("IntrestRate", IntrestRate),
                                          new XElement("LoanAmount", LoanAmount),
                                           new XElement("Payment", Payment),
                                        new XElement("Price", Price),
                                        new XElement("DownPayment", DownPayment)));
                    xdoc.Save(isostream);
                    isostream.Close();
                }
                else
                {
                    isostream = new IsolatedStorageFileStream("TaxDetails.xml", FileMode.Open, isostore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();

                    //var Details = from t in xdoc.Descendants("TermOfLoan")
                    //              select t;

                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in saveSettings Method In IsolatedSettings file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
               
            }
        }
        public List<Trems> LoadDetails()
        {
            MortageProperties prop = new MortageProperties();
            try
            {
                if (isostore.FileExists("TaxDetails.xml"))
                {
                    isostream = new IsolatedStorageFileStream("TaxDetails.xml", FileMode.Open, isostore);
                    xdoc = XDocument.Load(isostream);
                    isostream.Close();

                    var elements = from t in xdoc.Descendants("TaxDetails") select t;

                    foreach (XElement ele in elements)
                    {

                        prop.IntrestRate = Convert.ToDouble(ele.Element("IntrestRate").Value);
                        prop.LoanAmount = Convert.ToDouble(ele.Element("LoanAmount").Value);
                        prop.Payment = Convert.ToDouble(ele.Element("Payment").Value);
                        prop.Price = Convert.ToDouble(ele.Element("Price").Value);
                        prop.TermOfLoan = Convert.ToInt32(ele.Element("TermOfLoan").Value);
                        prop.DownPayment = Convert.ToDouble(ele.Element("DownPayment").Value);

                    }
                }

            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in saveSettings Method In IsolatedSettings file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
            }
            return LoadTerms(prop);
        }
        public List<Trems> LoadTerms(MortageProperties prop)
        {
            List<Trems> p = new List<Trems>();
            try
            {
                for (int i = 1; i <= (prop.TermOfLoan); i++)
                {
                    Trems t = new Trems();
                    t.Month = i;
                    double Intrest = (prop.LoanAmount) * (prop.IntrestRate) / (1200);
                    t.Intrest = Math.Round(Intrest, 2);

                    double Principal = prop.Payment - Intrest;
                    t.Principal = Math.Round(Principal, 2);

                    double Balance = (prop.LoanAmount) - Principal;
                    t.Balance = Math.Round(Balance, 2);
                    prop.LoanAmount = Math.Round(Balance, 2);
                    p.Add(t);

                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in saveSettings Method In IsolatedSettings file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
            }
            return p;
        }
    }

}
